﻿using System;
using System.IO;
using System.Windows.Forms;
using Relocation.Base;
using Relocation.Base.UI;

namespace Relocation
{
	public partial class FileWindow : BaseForm
	{
		public string strPath;

		public int iLeft;
		public int iTop;
		public int iRight;
		public int iBottom;

		public FileWindow()
		{
			InitializeComponent();
		}
		public FileWindow(Session s)
		{
			InitializeComponent();
			this.Session = s;

			iLeft = 0;
			iTop = 0;
			iRight = 0;
			iBottom = 0;

			WebTwainThum.MouseShape = true;
			WebTwainThum.MaxImagesInBuffer = 5;
			WebTwainThum.SetViewMode(1, 3);

			WebTwain.MaxImagesInBuffer = 1;
			WebTwain.SetViewMode(-1, -1);
			WebTwain.IfFitWindow = true;
			WebTwain.MouseShape = false;

			int num;
			for (num = 0; num < WebTwain.SourceCount; num++)
			{
				scannerType.Items.Add(WebTwain.get_SourceNameItems(Convert.ToInt16(num)));
				this.scannerType.SelectedIndex = 0;
			}

			UpdateImageInfo();
		}

		private void UpdateImageInfo()
		{
			Clipboard.Clear();
			WebTwainThum.CopyToClipboard(WebTwainThum.CurrentImageIndexInBuffer);
			WebTwain.RemoveAllImages();
			WebTwain.LoadDibFromClipboard();
			Clipboard.Clear();

			this.curPage.Text = Convert.ToString(WebTwainThum.CurrentImageIndexInBuffer + 1);
			this.allPage.Text = Convert.ToString(WebTwainThum.HowManyImagesInBuffer);
			this.scan.Enabled = true;

			if (WebTwainThum.HowManyImagesInBuffer == 0)
			{
				First.Enabled = false;
				Pre.Enabled = false;
				Next.Enabled = false;
				End.Enabled = false;
				remove.Enabled = false;
				removeAll.Enabled = false;
				switchLeft.Enabled = false;
				switchRight.Enabled = false;
				cut.Enabled = false;
				save.Enabled = false;
				WebTwain.RemoveAllImages();
				return;
			} else
			{
				remove.Enabled = true;
				removeAll.Enabled = true;
				switchLeft.Enabled = true;
				switchRight.Enabled = true;
				cut.Enabled = true;
				save.Enabled = true;
				WebTwain.IfFitWindow = true;
				if (WebTwainThum.CurrentImageIndexInBuffer >= 1)
				{
					First.Enabled = true;
					Pre.Enabled = true;
				} else
				{
					First.Enabled = false;
					Pre.Enabled = false;
				}
				if (WebTwainThum.CurrentImageIndexInBuffer < WebTwainThum.HowManyImagesInBuffer - 1)
				{
					Next.Enabled = true;
					End.Enabled = true;
				} else
				{
					Next.Enabled = false;
					End.Enabled = false;
				}
				if (WebTwainThum.HowManyImagesInBuffer == 5)
				{
					this.scan.Enabled = false;
				}
				if (WebTwainThum.HowManyImagesInBuffer < 5)
				{
					this.scan.Enabled = true;
				}
			}
		}


		private void CheckIfImagesInBuffer()
		{
			if (WebTwainThum.HowManyImagesInBuffer == 0)
			{
				MessageBox.Show("没有扫描文件！");
				return;
			}
		}

		private void remove_Click(object sender, EventArgs e)
		{
			CheckIfImagesInBuffer();
			WebTwainThum.RemoveImage(WebTwainThum.CurrentImageIndexInBuffer);
			if (WebTwainThum.HowManyImagesInBuffer == 0)
			{
				this.allPage.Text = Convert.ToString(WebTwainThum.HowManyImagesInBuffer);
				this.curPage.Text = "";
			} else
			{
				this.allPage.Text = Convert.ToString(WebTwainThum.HowManyImagesInBuffer);
				this.curPage.Text = Convert.ToString(WebTwainThum.CurrentImageIndexInBuffer + 1);
			}
			UpdateImageInfo();
		}

		private void removeAll_Click(object sender, EventArgs e)
		{
			CheckIfImagesInBuffer();
			this.allPage.Text = "0";
			this.curPage.Text = "";
			WebTwainThum.RemoveAllImages();
			UpdateImageInfo();
		}

		private void scan_Click(object sender, EventArgs e)
		{
			WebTwainThum.SelectSourceByIndex(Convert.ToInt16(scannerType.SelectedIndex));
			WebTwainThum.IfShowUI = true;
			WebTwainThum.CloseSource();
			WebTwainThum.OpenSource();
			WebTwainThum.PixelType = 2;
			WebTwainThum.IfDisableSourceAfterAcquire = true;
			WebTwainThum.Resolution = 150;
			WebTwainThum.IfFeederEnabled = true;
			WebTwainThum.IfAutoFeed = true;
			WebTwainThum.XferCount = -1;
			WebTwainThum.AcquireImage();
		}

		private void save_Click(object sender, EventArgs e)
		{
			strPath = Application.StartupPath;
			if (strPath.EndsWith(@"\"))
			{
				strPath = strPath + @"Files\";
			} else
			{
				strPath = strPath + @"\Files\";
			}
			if (Directory.Exists(strPath) == false)
			{
				Directory.CreateDirectory(strPath);
			}
			strPath = strPath + Convert.ToString(DateTime.Now.ToFileTime()) + ".pdf";
			WebTwainThum.SaveAllAsPDF(strPath);
			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void First_Click(object sender, EventArgs e)
		{
			CheckIfImagesInBuffer();
			WebTwainThum.CurrentImageIndexInBuffer = 0;
			UpdateImageInfo();
		}

		private void Pre_Click(object sender, EventArgs e)
		{
			CheckIfImagesInBuffer();
			if (WebTwainThum.CurrentImageIndexInBuffer == 0)
				return;
			WebTwainThum.CurrentImageIndexInBuffer = Convert.ToInt16(WebTwainThum.CurrentImageIndexInBuffer - 1);
			UpdateImageInfo();
		}

		private void Next_Click(object sender, EventArgs e)
		{
			CheckIfImagesInBuffer();
			if (WebTwainThum.CurrentImageIndexInBuffer == WebTwainThum.HowManyImagesInBuffer - 1)
				return;
			WebTwainThum.CurrentImageIndexInBuffer = Convert.ToInt16(WebTwainThum.CurrentImageIndexInBuffer + 1);
			UpdateImageInfo();
		}

		private void End_Click(object sender, EventArgs e)
		{
			CheckIfImagesInBuffer();
			WebTwainThum.CurrentImageIndexInBuffer = Convert.ToInt16(WebTwainThum.HowManyImagesInBuffer - 1);
			UpdateImageInfo();
		}

		private void cut_Click(object sender, EventArgs e)
		{
			CheckIfImagesInBuffer();
			if (iLeft == 0 && iRight == 0 && iTop == 0 && iBottom == 0)
			{
				return;
			} else
			{
				WebTwainThum.Crop(WebTwainThum.CurrentImageIndexInBuffer, iLeft, iTop, iRight, iBottom);
				iLeft = 0;
				iTop = 0;
				iRight = 0;
				iBottom = 0;
			}
			UpdateImageInfo();
		}

		private void switchLeft_Click(object sender, EventArgs e)
		{
			CheckIfImagesInBuffer();
			WebTwainThum.RotateLeft(WebTwainThum.CurrentImageIndexInBuffer);
			WebTwain.RotateLeft(WebTwain.CurrentImageIndexInBuffer);
		}

		private void switchRight_Click(object sender, EventArgs e)
		{
			CheckIfImagesInBuffer();
			WebTwainThum.RotateRight(WebTwainThum.CurrentImageIndexInBuffer);
			WebTwain.RotateRight(WebTwain.CurrentImageIndexInBuffer);
		}

		private void WebTwain_OnImageAreaSelected(object sender, AxDYNAMICWEBTWAINCTRLLib._IDynamicWebTwainEvents_OnImageAreaSelectedEvent e)
		{
			iLeft = e.left;
			iTop = e.top;
			iRight = e.right;
			iBottom = e.bottom;

			First.Enabled = false;
			Pre.Enabled = false;
			Next.Enabled = false;
			End.Enabled = false;
			remove.Enabled = false;
			removeAll.Enabled = false;
			switchLeft.Enabled = false;
			switchRight.Enabled = false;
			save.Enabled = false;
			scan.Enabled = false;
		}

		private void WebTwain_OnImageAreaDeselected(object sender, AxDYNAMICWEBTWAINCTRLLib._IDynamicWebTwainEvents_OnImageAreaDeselectedEvent e)
		{
			iLeft = 0;
			iTop = 0;
			iRight = 0;
			iBottom = 0;

			First.Enabled = true;
			Pre.Enabled = true;
			Next.Enabled = true;
			End.Enabled = true;
			remove.Enabled = true;
			removeAll.Enabled = true;
			switchLeft.Enabled = true;
			switchRight.Enabled = true;
			scan.Enabled = true;
			save.Enabled = true;
			UpdateImageInfo();
		}

		private void WebTwainThum_OnPostTransfer(object sender, EventArgs e)
		{
			this.allPage.Text = Convert.ToString(WebTwainThum.HowManyImagesInBuffer);
			this.curPage.Text = Convert.ToString(WebTwainThum.CurrentImageIndexInBuffer + 1);
			UpdateImageInfo();
		}

		private void WebTwainThum_OnMouseClick(object sender, AxDYNAMICWEBTWAINCTRLLib._IDynamicWebTwainEvents_OnMouseClickEvent e)
		{
			this.curPage.Text = Convert.ToString(e.sImageIndex + 1);
			WebTwainThum.CurrentImageIndexInBuffer = e.sImageIndex;
			UpdateImageInfo();
		}
	}
}
